//倒计时器
class TimerCountDown extends egret.Sprite{
    private tx:egret.TextField;
    private timer:egret.Timer;
    private overTime:Date;
    private str:string;
    private txColor:any;

    public constructor(_str:string="",_color:any=0xFFFFFF){
        super();
        this.str = _str;
        this.txColor = _color;
        this.tx = new egret.TextField();
        this.tx.size = 22;
        this.tx.fontFamily = "微软雅黑";
        this.tx.textColor = this.txColor;
        this.tx.text = "自动解散房间还剩 00:00:00"
        this.addChild(this.tx);
        
        this.timer = new egret.Timer(1000);
        this.timer.addEventListener(egret.TimerEvent.TIMER,this.updateTime,this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.remove,this);
    }
    public startTimer(_overTime:any){
        this.overTime = new Date(_overTime);
        this.NewTimwe(_overTime);
        this.timer.start();
    }

    private updateTime(e:egret.TimerEvent){
        this.NewTimwe(this.overTime);
    }
    private NewTimwe(_overTime:any){
        var newtime:number = new Date(Number(this.overTime)).getTime()-new Date().getTime();
        this.tx.text = this.str+this.getTimeStr(newtime);
        //console.log("\n\n我在计时: "+newtime);
        if(newtime <= 0){
            this.timer.stop();
            this.tx.text = this.str+"00:00:00";
            this.dispatchEventWith("timeOver");
           // console.log("自动解散时间到");
        }
    }
    public getTimeStr(num:number) {
        num /= 1000;
        return ("00"+Math.floor(num/3600)).substr(-2) + ":" + ("00"+Math.floor(num/60) % 60).substr(-2) + ":" + ("00"+Math.floor(num%60)).substr(-2) ;
    }

    private remove(){
        this.timer.removeEventListener(egret.TimerEvent.TIMER,this.updateTime,this);
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE,this.remove,this);
        this.timer.reset();
        this.timer.stop();
        this.timer = null;
       // console.log("自动解散房间倒计时已清除");
    }
}